﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using NLBS.Framework;
using System.Xml;
using System.Web;
using NLBS.Utility;
using System.IO;

namespace NLBS.Resources
{
   /// <summary>
   /// 多国语言
   /// </summary>
   public class Language
    {
       private static Dictionary<string,string> languages;
       private static Language language;

       public static Language Instance()
       {

           if (language == null)
           {
               language = new Language();
           }
           languages = language.getLanguage();

           return language;
       }

      

       public string GetResource(string key)
       {
           return languages[key];
       }


       /// <summary>
       /// 获取语言包的Key/Value
       /// </summary>
       /// <returns></returns>
       public Dictionary<string, string> getLanguage()
       {
           Dictionary<string, string> t;
           ContentCache cache = ContentCache.Instantiate();
           if (cache["nlbslanguage"] == null)
           {
               t = new Dictionary<string, string>();
               string lang = BlogInfo.Instance().Language;
               string dir = HttpContext.Current.Server.MapPath(string.Format("/App_Data/Language/{0}", lang));
               FileInfo[] files = FileHelp.GetFiles(dir);
               foreach (FileInfo file in files)
               {
                   XmlDocument doc = new XmlDocument();
                   doc.Load(file.FullName);
                   XmlNode firstNode = doc.DocumentElement;
                   foreach (XmlNode item in firstNode.ChildNodes)
                   {
                       if (item.NodeType == XmlNodeType.Element)
                       {
                           t[item.Attributes["name"].Value] = item.InnerText;
                       }
                   }  

               }
               //cache.Insert("nlbslanguageCount", files.Length, CacheDuration.Long);
               cache.Insert("nlbslanguage", t,CacheDuration.Long);
           }
           else
           {
               t=(Dictionary<string, string>)cache["nlbslanguage"];
           }
           return t;
       }
    }
}
